var WGame = {
	Version: 1, //版本

	SaveName: "",
	SavePath: "",
	/**
	 * @type {World.SQLiteSource}
	 */
	SaveDB: null,
	RunTime: 0,
};

var WConfig = {
	SpawnPoints: [
		Vector(0, 0, 0),
	],
};

class WPlayerData {
	Account = "";

	IsAccountProcessing = false;
	IsLoggedIn = false;
}

function OnScriptLoad() {
	World.Core.LoadScript("World/World.Function.js");
	World.Core.LoadScript("World/World.Extend.js");

	World.AI.SetActive(true);
	World.Core.SetHostName(`[color=#f01c6c]${GetLanguageText("Mod.Name")} | 测试服务器[/color]`);
	World.Core.SetMaxPlayers(16);
	World.Core.SetWorldTime(12, 0);
	World.Core.SetWorldTimeRate(1);
	World.Core.SetWorldWeather(-1);
	World.Core.SetHostIcon(3);

	World.Core.RegisterKeyListener("World_Use");
	World.Core.RegisterKeyListener("Vehicle_Horn");
	World.Core.RegisterKeyListener("Vehicle_Alarm");
	World.Core.RegisterKeyListener("Vehicle_Light");

	//Core
	CreateWorld();

	World.Timer.Create(LoopTimerSecond, 1, 0);
}

function OnScriptUnload() {
	Debug.LogError("OnScriptUnload");

	for (let i = 0; i < World.Player.GetCount(); i++) {
		let player = World.Player.Get(i);
		OnPlayerPart(player, 0);
	}
}

/**
 * @param {World.Player} player
 */
function OnPlayerComplete(player) {
	Message(`[color=#ffff00]玩家：${player.Name} 正在连接服务器！[/color]`);

	//Custom Data
	player.Data = new WPlayerData();
	player.Color = Random.RangeColor(200, 255);

	player.SetVoiceMode(1);

	//为玩家展示登陆窗口
	player.NetworkRPC("account_panel_show");

	MessagePlayer(player, "欢迎来到 [b]后世界时光[/b] 测试服务器！");
	MessagePlayer(player, "在开始游戏之前，请先登陆&注册账户！");
}

/**
 * @param {World.Player} player
 * @param {int} reason
 */
function OnPlayerPart(player, reason) {
	if (player.Data.IsLoggedIn) {
		SavePlayerData(player);
	}

	Message(`[color=#ffff00]玩家：${player.Name} 离开了服务器！[/color]`);
}

/**
 * @param {World.Player} player
 * @param {string} text
 * @returns {int}
 */
function OnPlayerChat(player, text) {
	World.Core.Message(`[color=${Misc.ColorToHEX(player.Color, false)}]${player.Name}: ${FormatRichText(text, true)}[/color]`);
	return 0;
}

/**
 * @param {World.Player} player
 * @param {string} cmd
 * @param {any} arg0
 * @param {any} arg1
 * @param {any} arg2
 */
function OnPlayerNetworkCMD(player, cmd, arg0, arg1, arg2) {
	if (cmd == "account_login") {
		PlayerAccount(player, arg0, arg1);
	} else if (cmd == "request_focus_use") {
		if (player.Entity && player.FocusEntity) {
			if (player.FocusEntity.IsVehicle) {
				if (player.Entity.IsPassenger == false) {
					player.Entity.EnterVehicle(player.FocusEntity.AsVehicle);
				}
			}
		}
	}
}

/**
 * @param {World.Player} player
 * @param {string} cmd
 * @param {string} arg
 */
function OnPlayerCommand(player, cmd, arg) {
	if (cmd == "save") {
		SaveGameData();
		MessagePlayer(player, "[color=#ffff00]数据已保存！[/color]");
	}
}

/**
 * @param {World.Player} player 
 */
function OnPlayerAccountLogged(player) {
	Message(`[color=#ffff00]玩家：${player.Name} 加入到世界！[/color]`);

	SpawnPlayerCharacter(player);
}

/**
 * @param {World.Player} player
 * @param {string} keyTag
 */
function OnPlayerKeyDown(player, keyTag) {
	if (keyTag == "World_Use") {
		if (player.Character.Vehicle != null) {
			player.Character.Eject();
		}
	} else if (keyTag == "Vehicle_Horn") {
		if (player.Character.Vehicle != null) {
			player.Character.Vehicle.Horn = true;
		}
	} else if (keyTag == "Vehicle_Alarm") {
		if (player.Character.Vehicle != null) {
			player.Character.Vehicle.Alarm = !player.Character.Vehicle.Alarm;
		}
	} else if (keyTag == "Vehicle_Light") {
		if (player.Character.Vehicle != null) {
			if (player.Character.Vehicle.Light + 1 <= 2) {
				player.Character.Vehicle.Light = player.Character.Vehicle.Light + 1;
			} else {
				player.Character.Vehicle.Light = 0;
			}
		}
	}
}

/**
 * @param {World.Player} player
 * @param {string} keyTag
 */
function OnPlayerKeyUp(player, keyTag) {
	if (keyTag == "Vehicle_Horn") {
		if (player.Character.Vehicle != null) {
			player.Character.Vehicle.Horn = false;
		}
	}
}

function LoopTimerSecond() {
	WGame.RunTime++;
}